package org.groupg.practice.guiPractice.awt;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultiListener {
    private final Frame f = new Frame("测试");
    private final TextArea ta = new TextArea(6, 40);
    private final Button b1 = new Button("按钮一");
    private final Button b2 = new Button("按钮二");

    public static void main(String[] args) {
        new MultiListener().init();
    }

    public void init() {
        // 创建FirstListener监听器的实例
        var fl = new FirstListener();
        // 给b1按钮注册两个事件监听器
        b1.addActionListener(fl);
        b1.addActionListener(new SecondListener());
        // 将f1事件监听器注册给b2按钮
        b2.addActionListener(fl);
        f.add(ta);
        var p = new Panel();
        p.add(b1);
        p.add(b2);
        f.add(p, BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
    }

    class FirstListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ta.append("第一个事件监听器被触发，事件源是："
                    + e.getActionCommand() + "\n");
        }
    }

    class SecondListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ta.append("单击了“"
                    + e.getActionCommand() + "”按钮\n");
        }
    }
}